package org.geohunter.activity.osmmap;

import org.andnav.osm.ResourceProxy;
import org.andnav.osm.views.overlay.OpenStreetMapViewOverlay;
import org.andnav.osm.views.overlay.OpenStreetMapViewSimpleLocationOverlay;
import org.andnav.osm.views.util.OpenStreetMapRendererInfo;
import org.geohunter.GeoFixProvider;
import org.geohunter.GeocacheFilter;
import org.geohunter.GraphicsGenerator;
import org.geohunter.R;
import org.geohunter.Toaster;
import org.geohunter.activity.filterlist.FilterTypeCollection;
import org.geohunter.activity.main.GuiState;
import org.geohunter.activity.map.CacheHint;
import org.geohunter.activity.map.DensityOverlayDelegate;
import org.geohunter.activity.map.MapTabDI;
import org.geohunter.activity.map.OverlayManager;
import org.geohunter.database.CachesProviderDb;
import org.geohunter.database.DbFrontend;
import org.geohunter.menuactions.MenuActionSettings;
import org.geohunter.menuactions.MenuActions;
import org.geohunter.task.DelayingTaskRunner;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.widget.RelativeLayout;

import java.util.ArrayList;

public class OsmMapTabDI {

    public static OsmMapTab create(Activity activity, GuiState guiState,
            GeoFixProvider geoFixProvider, DbFrontend dbFrontend,
            GraphicsGenerator graphicsGenerator,
            FilterTypeCollection filterTypeCollection) {
        final RelativeLayout layout = new RelativeLayout(activity);
        Drawable icon = activity.getResources().getDrawable(R.drawable.ic_menu_mapmode);
        ResourceProxy resourceProxy = new ResourceProxyImpl(activity);
        GeocacheFilter cacheFilter = guiState.getActiveFilter();
        final CachesProviderDb cachesProviderArea = new CachesProviderDb(dbFrontend, true);
        cachesProviderArea.setFilter(cacheFilter);
        final Toaster tooManyCachesToaster = new Toaster(activity, 
                R.string.too_many_caches, false);
        final Toaster densityViewToaster = new Toaster(activity, 
                R.string.density_view_switch, false);
        DensityOverlayDelegate densityOverlayDelegate = MapTabDI.createDensityOverlayDelegate();
        OsmDensityOverlay densityOverlay = new OsmDensityOverlay(resourceProxy, densityOverlayDelegate);
        
        ArrayList<OsmPinItem> empty = new ArrayList<OsmPinItem>();
        OpenStreetMapViewOverlay emptyPins = new OsmPinOverlay(empty, null, resourceProxy);

        OpenStreetMapViewSimpleLocationOverlay myLocationOverlay =
            new OpenStreetMapViewSimpleLocationOverlay(activity, resourceProxy);
        OsmMapView mapView = new OsmMapView(activity, 
                OpenStreetMapRendererInfo.MAPNIK, emptyPins, myLocationOverlay);
        final CacheHint cacheHint = new CacheHint(layout);
        
        OsmCachePinsOverlayFactory factory = 
            new OsmCachePinsOverlayFactory(graphicsGenerator, dbFrontend, 
                    cacheHint, guiState, resourceProxy);
        DelayingTaskRunner delayingTaskRunner = new DelayingTaskRunner();
        
        OverlayManager<OpenStreetMapViewOverlay> manager =
            new OverlayManager<OpenStreetMapViewOverlay>(mapView,
                    densityOverlay, false,
                    cachesProviderArea,
                    cacheFilter, tooManyCachesToaster,
                    densityViewToaster,
                    factory,
                    cacheHint, densityOverlayDelegate,
                    delayingTaskRunner);

        //Lousy design that requires extra init
        mapView.setOverlayManager(manager);

        // *** BUILD MENU ***
        final MenuActions menuActions = new MenuActions();
        Resources resources = activity.getResources();
        menuActions.add(new MenuActionOsmCenterLocation(resources,
                mapView.getController(),
                myLocationOverlay));
        menuActions.add(new MenuActionSettings(activity, resources));
        
        return new OsmMapTab(layout, icon, menuActions, guiState, 
                geoFixProvider, mapView, myLocationOverlay, manager);
    }
}
